package com.yanqu.road.server.manger.cricket.skillhandler.attround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.attround.CricketSkillAttRoundStrategy;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill403Strategy implements CricketSkillAttRoundStrategy {

    /**
     * 技能处理-张飞-醉怒强袭：张飞酒后狂怒，{0}的命中率对敌方造成最大生命值{1}的伤害。
     *
     * @param cricketGame        本局对局对象
     * @param skillData          触发的技能
     * @param baseAttSkill       基础攻击技能
     * @param baseAttReHpResult  基础攻击结果对象
     * @param action             我方
     * @param target             对方
     * @param attackRoundBuilder 返回PB结构体
     * @return true：死亡，退出技能处理，false：继续技能处理
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, SkillData baseAttSkill, AddHpResult baseAttReHpResult, BattleData action, BattleData target, CricketProto.CricketAttackRound.Builder attackRoundBuilder, long battleFrameId) {
        //基础攻击miss，不触发技能
        if (baseAttReHpResult == null) {
            return false;
        }
        // 张飞酒后狂怒，{0}的命中率对敌方造成最大生命值{1}的伤害。
        if (baseAttSkill.getSkillEffect().getSkillType() == eSkillType.CricketBattleSkill137.getValue() && action.getParam() >= skillData.getSkillEffect().getP1()) {//1快夹2冲夹3举摔
            if (new RandomHelper().next(1000) < skillData.getSkillEffect().getP2()) {
                // 造成伤害
                skillData.effectAmountInc();
                CricketProto.CricketBattleFrame.Builder skill403FrameBuilder = cricketGame.newBattleFrameBuilder();
                skill403FrameBuilder.setActionUserId(action.getUserId());
                skill403FrameBuilder.setTargetUserId(target.getUserId());
                skill403FrameBuilder.setSkillId(skillData.getSkillId());
                //附加伤害
                long value = BigDecimal.valueOf(action.getMaxHp()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP3())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
                AddHpResult skill168AddHpResult = target.addHp(-value);
                skill403FrameBuilder.setAttack(skill168AddHpResult.getValue());
                skill403FrameBuilder.setIsKill(skill168AddHpResult.isKill());
                skill403FrameBuilder.setHarmEffectSkillId(skill168AddHpResult.getReduceHpEffectSkillId());
                skill403FrameBuilder.setIsMiss(skill168AddHpResult.isMissFlag());
                cricketGame.fillRemainHp(skill403FrameBuilder);
                skill403FrameBuilder.setParam(action.getParam());
                attackRoundBuilder.addAttack(skill403FrameBuilder);
                if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.Attack.getValue(), skill403FrameBuilder.getBattleFrameId())) {
                    return true;
                }
            }
        }
        return false;
    }
}
